﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;

namespace KhoBanHang
{
    public partial class DonDatNCC : Form
    {
        #region Bien cuc bo
        List<Business.DonDatNCC> danhsachDDNCC;
        List<HangHoa> danhSachHangHoa;

        Business.DonDatNCC DonDat;
        Business.HangHoa hangHoa;
        #endregion

        #region Load Thong Tin
        public DonDatNCC()
        {
            InitializeComponent();
        }

        private void DonDatNCC_Load(object sender, EventArgs e)
        {
            LoadDanhSachHang();
            LoadDanhsachDonDat(true);

            dtpickerTuNgayDat.CustomFormat = "dd/MM/yyyy";
            dtpickerDenNgayDat.CustomFormat = "dd/MM/yyyy";
            dtpickerTuNgayGiao.CustomFormat = "dd/MM/yyyy";
            dtpickerDenNgayGiao.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayDat.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayGiao.CustomFormat = "dd/MM/yyyy";

            txtNVDat.Text = "NV2";
            if (@Session.quyen == "3") 
            {
                btnThem.Enabled = false;
                btnXoa.Enabled=false;
                btnThemCTDD.Enabled = false;
                btnLuuCTDD.Enabled = false;
                btnXoaCTDD.Enabled = false;
                btnLuuCTDD.Enabled = false;
                
            }
                
        }

        private void LoadDanhSachHang()
        {
            danhSachHangHoa = Business.HangHoa.LayDanhSach();

            cbxTenHang.DataSource = danhSachHangHoa;
            cbxTenHang.DisplayMember = "TenHang";
            cbxTenHang.ValueMember = "MaHangHienThi";

            cbxTenHang.SelectedItem = null;
        }

        private void LoadDanhsachDonDat(bool lanDau)
        {
            if (lanDau)
            {
                danhsachDDNCC = Business.DonDatNCC.LayDanhSach();
            }
            else
            {
                if (rbtnNgayDat.Checked && dtpickerTuNgayDat.Value != null)
                    danhsachDDNCC = Business.DonDatNCC.LayDanhSach_NgayDat(dtpickerTuNgayDat.Value, dtpickerDenNgayDat.Value);
                else if (rbtnNgayGiao.Checked && dtpickerTuNgayGiao.Value != null)
                    danhsachDDNCC = Business.DonDatNCC.LayDanhSach_NgayGiao(dtpickerTuNgayGiao.Value, dtpickerDenNgayGiao.Value);
                else if (rbtnMaDD.Checked && txtMaDD.Text.Contains("DDNCC"))
                    danhsachDDNCC = Business.DonDatNCC.LayDanhSach_MaDonDat(txtMaDD.Text);
                else
                    danhsachDDNCC = Business.DonDatNCC.LayDanhSach();
            }

            gridviewDSDD.DataSource = danhsachDDNCC;
            gridviewDSDD.Columns["MaDonDatNCC"].Visible = false;
            gridviewDSDD.Columns["NgayDat"].DefaultCellStyle.Format = "dd/MM/yyyy";
            gridviewDSDD.Columns["NgayGiao"].DefaultCellStyle.Format = "dd/MM/yyyy";
            gridviewDSDD.Columns["SoTien"].DefaultCellStyle.Format = "##,###,###";

            gridviewDSDD.Columns["MaDonDatNCCHienThi"].HeaderText = "Mã đơn đặt";
            gridviewDSDD.Columns["NgayDat"].HeaderText = "Ngày đặt";
            gridviewDSDD.Columns["NgayGiao"].HeaderText = "Ngày giao";
            gridviewDSDD.Columns["SoTien"].HeaderText = "Số tiền";
            gridviewDSDD.Columns["GhiChu"].HeaderText = "Ghi chú";
        }

        public void LoadDanhSachChiTietDonDat()
        {
            BindingSource source = new BindingSource();
            source.DataSource = DonDat.DanhSachChiTietDonDat;

            gridviewChiTietLapđ.DataSource = source;
            gridviewChiTietLapđ.RefreshEdit();
            gridviewChiTietLapđ.Refresh();

            gridviewChiTietLapđ.Columns["MaCTDDH"].Visible = false;
            gridviewChiTietLapđ.Columns["MaHang"].Visible = false;
            gridviewChiTietLapđ.Columns["MaDDH"].Visible = false;
            gridviewChiTietLapđ.Columns["MaCTDDHHienThi"].HeaderText = "Mã chi tiết đặt";
            gridviewChiTietLapđ.Columns["MaHangHienThi"].HeaderText = "Mã hàng";
            gridviewChiTietLapđ.Columns["TenHang"].HeaderText = "Tên hàng";
            gridviewChiTietLapđ.Columns["GiaNhap"].HeaderText = "Giá nhập";
            gridviewChiTietLapđ.Columns["SoLuong"].HeaderText = "Số lượng";

            gridviewChiTietLapđ.Columns["GiaNhap"].DefaultCellStyle.Format = "##,###,###";

            txtTongTien.Text = DonDat.TinhTongTienHang().ToString();
        }

        private void LoadThongTinDonDatHang(int maDDH)
        {
            DonDat = Business.DonDatNCC.LayDonDatNCC(maDDH);
            dtpickerNgayDat.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayDat.Text = DonDat.NgayDat.ToString();
            dtpickerNgayGiao.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayGiao.Text = DonDat.NgayGiao.ToString();
            txtMaDDH.Text = DonDat.MaDonDatNCCHienThi;            
            //txtMaDD.Text = 
            txtGhiChu.Text = DonDat.GhiChu;

            LoadDanhSachChiTietDonDat();
        }
        #endregion

        #region Event Handle
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                LoadDanhsachDonDat(false);
            }
        }

        private void cbxTenHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxTenHang.SelectedItem != null && DonDat != null)
            {
                hangHoa = danhSachHangHoa[cbxTenHang.SelectedIndex];
                txtSoLuongTon.Text = hangHoa.SoLuongTon.ToString();
                txtMahang.Text = hangHoa.MaHangHienThi.ToString();
            }
        }
        #endregion

        #region Thao tac don dat hang
        private void btnTim_Click(object sender, EventArgs e)
        {
            LoadDanhsachDonDat(false);
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            XoaTrangChiTietDonDat();
            if (danhsachDDNCC != null && danhsachDDNCC.Count > 0)
            {
                tabControl1.SelectedTab = this.CTDDtabPage;
                if (gridviewDSDD.SelectedRows.Count == 0)
                {
                    int maDDH = danhsachDDNCC[0].MaDonDatNCC;
                    LoadThongTinDonDatHang(maDDH);
                }
                else
                {
                    int maDDH = danhsachDDNCC[gridviewDSDD.SelectedRows[0].Index].MaDonDatNCC;
                    LoadThongTinDonDatHang(maDDH);
                }
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = CTDDtabPage;
            dtpickerNgayDat.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayGiao.CustomFormat = "dd/MM/yyyy";
            DonDat = new Business.DonDatNCC();
            txtMaDDH.Text = "";
            gridviewChiTietLapđ.DataSource = null;
            txtTongTien.Text = "";

            XoaTrangChiTietDonDat();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (DonDat.MaDonDatNCC == 0)
            {
                CapNhatThongTinChung();
                int maDDH = Business.DonDatNCC.ThemMoi(DonDat);

                LoadThongTinDonDatHang(maDDH);
                MessageBox.Show("Thêm mới đơn đặt hàng thành công");
            }
            else
            {
                CapNhatThongTinChung();
                Business.DonDatNCC.CapNhat(DonDat);

                LoadThongTinDonDatHang(DonDat.MaDonDatNCC);
                MessageBox.Show("Cập nhật đơn đặt hàng thành công");
            }

            XoaTrangChiTietDonDat();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (danhsachDDNCC != null && danhsachDDNCC.Count > 0)
            {
                if (gridviewDSDD.SelectedRows.Count > 0)
                {
                    int maDDH = danhsachDDNCC[gridviewDSDD.SelectedRows[0].Index].MaDonDatNCC;
                    Business.DonDatKH.Xoa(maDDH);

                    LoadDanhsachDonDat(false);
                }
            }
        }

        private void CapNhatThongTinChung()
        {
            DonDat.NgayDat = dtpickerNgayDat.Value;
            DonDat.NgayGiao = dtpickerNgayGiao.Value;
            DonDat.SoTien = double.Parse(txtTongTien.Text);
            DonDat.GhiChu = txtGhiChu.Text;
        }
        #endregion

        #region Thao tac chi tiet don dat hang
        bool dangThemMoi = true;
        private void gridviewChiTietLapdd_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ChiTietDonDatNCC chiTietDonDat = DonDat.DanhSachChiTietDonDat[e.RowIndex];
            txtMahang.Text = chiTietDonDat.MaHang.ToString();
            txtGiaNhap.Text = chiTietDonDat.GiaNhap.ToString();
            txtSoLuongMua.Text = chiTietDonDat.SoLuong.ToString();
            cbxTenHang.SelectedValue = chiTietDonDat.MaHangHienThi;
            dangThemMoi = false;
        }

        private void XoaTrangChiTietDonDat()
        {
            cbxTenHang.SelectedItem = null;
            txtSoLuongTon.Text = "";
            txtSoLuongMua.Text = "";
            txtMahang.Text = "";
            txtGiaNhap.Text = "";
            dangThemMoi = true;
        }

        private void btnThemCTDD_Click(object sender, EventArgs e)
        {
            XoaTrangChiTietDonDat();
        }

        private void btnLuuCTDD_Click(object sender, EventArgs e)
        {
            int maDDH = DonDat.MaDonDatNCC;
            int maHang = Int16.Parse(txtMahang.Text.Substring(2));
            string tenHang = ((HangHoa)cbxTenHang.SelectedItem).TenHang;
            int soLuong = Int16.Parse(txtSoLuongMua.Text);
            double giaBan = double.Parse(txtGiaNhap.Text);

            ChiTietDonDatNCC chiTietDonDat = new ChiTietDonDatNCC(DonDat.MaDonDatNCC, maHang, tenHang, soLuong, giaBan);
            if (dangThemMoi == true)
            {
                DonDat.ThemMoiChiTietDonDat(maDDH, maHang, tenHang, soLuong, giaBan);
            }
            else
            {
                if (gridviewChiTietLapđ.SelectedRows.Count > 0)
                {
                    int index = gridviewChiTietLapđ.SelectedRows[0].Index;
                    DonDat.CapNhatChiTietDonDat(index, maDDH, maHang, tenHang, soLuong, giaBan);
                }
            }

            LoadDanhSachChiTietDonDat();
            XoaTrangChiTietDonDat();
        }

        private void btnXoaCTDD_Click(object sender, EventArgs e)
        {
            int index = gridviewChiTietLapđ.SelectedRows[0].Index;
            DonDat.XoaChiTietDonDat(index);
            LoadDanhSachChiTietDonDat();
        }
        #endregion
    }
}
